package com.banshuibao.bsb_android.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

import com.banshuibao.bsb_android.R;


/**
 * Created by zyh on 2017/2/18.
 * <p>
 * 自带红点的textview
 */

public class RedTextView extends AppCompatTextView {

    private boolean redShow = false;

    private Context context;

    private int w;

    private int h;

    private int redPaddingLeft;
    private int redPaddingRight;
    private int redPaddingTop;
    private int redPaddingBottom;
    private int radius;
    private Paint paint = new Paint();

    public RedTextView(Context context) {
        super(context);
        this.context = context;
    }

    public RedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RedTextView);
        redPaddingTop = typedArray.getDimensionPixelSize(R.styleable.RedTextView_red_padding_top, 10);
        redPaddingBottom = typedArray.getDimensionPixelSize(R.styleable.RedTextView_red_padding_bottom, 0);
        redPaddingRight = typedArray.getDimensionPixelSize(R.styleable.RedTextView_red_padding_right, 10);
        redPaddingLeft = typedArray.getDimensionPixelSize(R.styleable.RedTextView_red_padding_left, 0);
        radius = typedArray.getDimensionPixelSize(R.styleable.RedTextView_radius, 10);
        typedArray.recycle();
    }

    public void setRedShow(boolean b) {
        if (this.redShow == b)
            return;
        this.redShow = b;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (redShow) {
            paint.setColor(context.getResources().getColor(R.color.consultantRed));
            if (redPaddingLeft != 0)
                canvas.drawCircle(redPaddingLeft, redPaddingTop, radius, paint);
            else
                canvas.drawCircle(w - redPaddingRight, redPaddingTop, radius, paint);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.w = w;
        this.h = h;
    }
}
